package com.fau.hm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalGroup;
import com.fau.hm.entity.MedicalPlatform;
import com.fau.hm.service.IDeptManagerService;
import com.fau.hm.util.Page;
import com.fau.hm.util.ParamUtil;
import com.fau.hm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class DeptManagerService extends BaseService implements IDeptManagerService {

	@Override
	public Page findPlatformByEid(Map<String, Object> map, MedicalPlatform platform, MedicalAdmin admin) {
		StringBuilder statusSql = new StringBuilder("from MedicalPlatform a where 1=1 and a.eid = ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalPlatform a where 1=1 and a.eid = ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		//排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils .isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])){
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]); 
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		return this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
	}

	@Override
	public Page findGroupByEid(Map<String, Object> map, MedicalGroup group, MedicalAdmin admin) {
		StringBuilder statusSql = new StringBuilder("from MedicalGroup a where 1=1 and a.eid = ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalGroup a where 1=1 and a.eid = ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		//排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils .isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])){
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]); 
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		return this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean savePlatform(MedicalPlatform platform, MedicalAdmin admin) {
		platform.setEid(admin.getEnterpriseId());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(platform);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(platform);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveEditPlatform(MedicalPlatform medicalPlatform) {
		MedicalPlatform platform = (MedicalPlatform) this.iBaseDao.getObject(MedicalPlatform.class, medicalPlatform.getId());
		platform.setName(medicalPlatform.getName());
		platform.setDescription(medicalPlatform.getDescription());
		platform.setPlatformType(medicalPlatform.getPlatformType());
		platform.setUpdateTime(new Date());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(platform);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(platform);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveGroup(MedicalGroup group, MedicalAdmin admin, String platformId) {
		MedicalPlatform platform = new MedicalPlatform();
		platform.setId(platformId);
		group.setPlatform(platform);
		group.setEid(admin.getEnterpriseId());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(group);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(group);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveEditGroup(MedicalGroup medicalGroup, String platformId) {
		MedicalGroup group = (MedicalGroup) this.iBaseDao.getObject(MedicalGroup.class, medicalGroup.getId());
		MedicalPlatform platform = new MedicalPlatform();
		platform.setId(platformId);
		group.setPlatform(platform);
		group.setName(medicalGroup.getName());
		group.setDescription(medicalGroup.getDescription());
		group.setUpdateTime(new Date());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(group);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(group);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delPlatform(MedicalPlatform medicalPlatform) {
		this.iBaseDao.deleteObject(medicalPlatform);
	}

	@Override
	public boolean includePlatfromGroup(String platformId) {
		int count = this.iBaseDao.getIntCount("select count(*) from MedicalGroup g where g.platform.id = ? ", platformId);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delGroup(MedicalGroup group) {
		this.iBaseDao.deleteObject(group);
	}

	@Override
	public boolean includeGroupUser(String groupId) {
		int count = this.iBaseDao.getIntCount("select count(*) from MedicalAdmin u where u.group.id = ? ", groupId);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalPlatform> findPlatformList(String eid) {
		return this.iBaseDao.getBySql("from MedicalPlatform m where m.eid = ? ", null, eid);
	}
	
	@Override
	public boolean includeDept(String eid, String name) {
		int count = this.iBaseDao.getIntCount("select count(*) from MedicalDepartment m where m.eid = ? and m.name = ? ", eid, name);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean includeGroup(String platformId, String name) {
		int count = this.iBaseDao.getIntCount("select count(*) from MedicalGroup m where m.platform.id = ? and m.name = ? ", platformId, name);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean includePlatform(String eid, String name, String type) {
		int count = this.iBaseDao.getIntCount("select count(*) from MedicalPlatform m where m.eid = ? and m.name = ? and m.platformType = ?", eid, name, type);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

}
